﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ZHDJ.Data.Models.Grassroots;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal.Grassroots
{
    public partial class Default2 : PortalPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        public void LoadData()
        {
            DataTable dt = PartyGrassrootsGroup.GetGroupItemData();
            List<GroupData> data = new List<GroupData>();
            if (dt.Rows.Count > 0)
            {
                DataRow[] rowGroup = dt.Select("ParentID=''");

                foreach (DataRow row in rowGroup)
                {
                    GroupData gd = new GroupData();
                    gd.Title = row["Name"].ToString();


                    IEnumerable<DataRow> qDatas = dt.AsEnumerable().Where<DataRow>(r =>
                    (r.Field<string>("ParentID").ToString() == row["ID"].ToString()));
                    var qg = from q in qDatas group q by q.Field<string>("TypeID") into g select g;
                    foreach (var it in qg)
                    {
                        GroupGQ gqM = new GroupGQ();
                        gqM.Title = qDatas.First(a => a.Field<string>("TypeID") == it.Key).Field<string>("TypeName");
                        foreach (var it2 in it)
                        {
                            GroupItem groupItem = new GroupItem();
                            groupItem.ID = it2.Field<string>("ID");
                            groupItem.Title = it2.Field<string>("Name");
                            groupItem.href = "ItemDetail.aspx?ItemID="+ it2.Field<string>("ID");

                            gqM.Items.Add(groupItem);

                        }
                        //添加更多选项
                        if (gqM.Items.Count >= 10)
                        {
                            for (int i = gqM.Items.Count-1; i >= 9; i--)
                            {
                                gqM.Items[i].InnerStyle = "display:none;";
                            }
                            
                            GroupItem moreItem = new GroupItem();
                            moreItem.ID = "more";
                            moreItem.Title = "更多";
                            moreItem.href = "javascript:void(0)";
                            moreItem.onclick = "ShowMore(this);return false;";
                            gqM.Items.Add(moreItem);


                        }
                        gd.Gq.Add(gqM);
                    }
                    data.Add(gd);
                }
            }
            var json = JsonConvert.SerializeObject(data);
            hdData.Value = json;
        }
    }

    public class GroupData
    {
        public GroupData()
        {
            Gq = new List<GroupGQ>();
        }
        public string Title { get; set; }
        public List<GroupGQ> Gq { get; set; }
    }

    public class GroupGQ
    {
        public GroupGQ()
        {
            Items = new List<GroupItem>();
        }

        public string Title { get; set; }
        public List<GroupItem> Items { get; set; }
    }

    public class GroupItem
    {
        public string ID { get; set; }
        public string Title { get; set; }

        public int OrderNo { get; set; }

        public string InnerStyle { get; set; }

        public string href { get; set; }

        public string onclick { get; set; }
    }
}